<template>
  <div class="content">
    <div class="scroll-view-box" v-loading="submitBtn">
      <el-form label-width="200px" :inline="true" :model="formData" :rules="formRules" ref="elForm"
        label-position="top">
        <el-form-item label="项目名称" prop="project_id">
          <el-select filterable v-model="formData.project_id" placeholder="请选择项目名称" clearable class="input-width">
            <el-option v-for="item in projectList" :key="item.id" :label="item.project_name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="增减项类型" prop="increase_or_decrease">
          <el-select filterable v-model="formData.increase_or_decrease" placeholder="请选择增减项类型" clearable
            class="input-width">
            <el-option label="增项" :value="1"></el-option>
            <el-option label="减项" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="增项预计开工日期" prop="phone" v-if="formInline.type == 1">
          <el-date-picker class="input-box" v-model="formInline.start_date" type="date" placeholder="选择预计开工日期" clearable
            value-format="yyyy-MM-dd" :picker-options="pickerOptionsEnd" @change="startDateChange">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="增项预计完工日期" prop="phone" v-if="formInline.type == 1">
          <el-date-picker class="input-box" :disabled="!formInline.start_date" v-model="formInline.end_date" type="date"
            placeholder="选择预计完工日期" clearable value-format="yyyy-MM-dd" :picker-options="pickerOptionsStart_"
            @change="endDateChange">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="增项预计工期" prop="phone" v-if="formInline.type == 1">
          <el-input v-model="formInline.day" :disabled="true" placeholder="请输入预计工期">
            <div slot="suffix" style="width: 25px;">天</div>
          </el-input>
        </el-form-item> -->
        <el-form-item label="增减内容" prop="change_content_idx">
          <el-select filterable multiple v-model="formData.change_content_idx" placeholder="请选择增减内容" clearable
            class="input-width">
            <el-option v-for="(item, idx) in contentList" :key="idx" :label="item.name" :value="idx">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="增减项原因" prop="reason_type">
          <el-input type="textarea" v-model="formData.reason_type" placeholder="请输入增减项原因" clearable
            class="input-width"></el-input>
        </el-form-item>
        <el-form-item label="施工内容" prop="construction_content">
          <el-input type="textarea" v-model="formData.construction_content" placeholder="请输入施工内容" clearable
            class="input-width"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable
            class="input-width"></el-input>
        </el-form-item>
        <el-form-item label="附件" prop="file_list">
          <div class="file-list-box">
            <div class="file-item-box" v-for="(item, index) in formData.file_list" :key="index">
              <div class="el-icon-delete-box" @click="delFileFunc(index)">
                <i class="el-icon-error"></i>
              </div>
              <el-tooltip class="item" effect="dark" :content="item.name" placement="top">
                <img class="img" :src="$fileImgFunc(item)" />
              </el-tooltip>
            </div>

            <el-upload class="avatar-uploader" action="#" :show-file-list="false" :auto-upload="false"
              list-type="picture-card" :on-change="changeFile">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveSubmit('elForm')" :loading="submitBtn">提交</el-button>
        <el-button @click="cancelFunc">关闭</el-button>
      </span>
    </div>
  </div>
</template>
<script>
import idiApi from '@/api/increaseDecreaseItemsApi'
import configApi from '@/api/configApi';

export default {
  name: "increaseDecreaseItemsAdd",
  data() {
    let validateFileList = (rule, value, callback) => {
      console.log(rule, value)
      if (!value.length) {
        callback(new Error('请选择附件'));
      } else {
        callback();
      }
    };
    return {
      // 关联数据
      projectList: [],
      contentList: [],

      // 提交内容
      formData: {
        project_id: '',
        increase_or_decrease: '',
        // expect_duration_days: '',
        // start_date: '',
        // end_date: '',
        reason_type: '',
        change_content_idx: [],
        construction_content: '',
        remark: '',
        file_list: [],
        del_file: []
      },
      formRules: {
        project_id: [{ required: true, message: "请选择项目名称", trigger: "change" }],
        increase_or_decrease: [{ required: true, message: "请选择增减项类型", trigger: "change" }],
        change_content_idx: [{ required: true, message: "请选择增减内容", trigger: "change" }],
        reason_type: [{ required: true, message: "请输入增减项原因", trigger: "change" }],
        construction_content: [{ required: true, message: "请输入施工内容", trigger: "change" }],
        file_list: [{ required: true, validator: validateFileList, trigger: "change" }]
      },
      submitBtn: false,
    };
  },
  computed: {
  },
  mounted() {
    this.getPorjectList()
    this.getChangeContent()
  },
  methods: {
    // 选择文件
    changeFile(file) {
      console.log(file)
      this.formData.file_list.push(file)
      // this.$videoPost(file, (e => {
      //   console.log(e, "文件")
      //   this.formData.file_list.push(e);
      // }));
    },
    delFileFunc(index) {
      // 删除已存在文件
      // this.formData.del_file = [];
      if (!this.formData.file_list[index].raw) {
        this.formData.del_file.push(this.formData.file_list[index].Id)
      }
      // 删除文件
      this.formData.file_list.splice(index, 1);
    },
    // 提交
    saveSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formData = this.formData
          let names = formData.change_content_idx.map(idx => this.contentList[idx].name)
          let ids = formData.change_content_idx.map(idx => this.contentList[idx].id)
          let data = {
            project_id: formData.project_id,
            increase_or_decrease: formData.increase_or_decrease,
            reason_type: formData.reason_type,
            change_content: names.join(','),
            change_content_id: ids,
            construction_content: formData.construction_content,
            remark: formData.remark,
          }
          let formdata = new FormData();
          formData.file_list.map(item => {
            formdata.append("change_items_file[]", item.raw);
          })
          formdata.append("data", JSON.stringify(data));

          this.submitBtn = true;
          idiApi.applyChangeItems(formdata).then(res => {
            console.log(res, '新增增减项')
            if (res.code == 200) {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.cancelFunc()
            } else {
              this.$message.error(res.message);
            }
            this.submitBtn = false;
          })
        } else {
          return false;
        }
      });
    },
    // 取消
    cancelFunc() {
      this.$goBackTab(this);
    },

    /****************获取关联数据*********************/
    // 获取相关项目
    getPorjectList() {
      idiApi.allowAssociatedProjects().then(res => {
        console.log(res, '相关项目列表')
        if (res.code == 200) {
          this.projectList = res.data
        }
      })
    },
    // 获取增减内容
    getChangeContent() {
      configApi.getList({
        page: 1,
        num: 999
      }, 4).then(res => {
        console.log(res, '获取增减内容')
        if (res.code == 200) {
          this.contentList = res.data.list
        }
      })
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/addPage.scss";
</style>